import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

public class Draw extends JPanel implements ActionListener
{
	Timer mainTimer;
	Player player;
	Enemy enemy;
	
	int enemyCount = 5;
	
	static ArrayList<Enemy> enemies = new ArrayList<Enemy>();	
	static ArrayList<Bullet> bullets = new ArrayList<Bullet>();
	
	Random rand = new Random();
	
	public Draw()
	{
		setFocusable(true);
		
		player = new Player(400, 510);
		addKeyListener(new KeyAdapt(player));
		
		
	
		mainTimer = new Timer(10, (ActionListener) this); // 10 ms
		mainTimer.start();
		
		startGame();
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		
		player.draw(g2);
		
		for (int i = 0; i < enemies.size(); i++)
		{
			Enemy tempEnemy = enemies.get(i);
			tempEnemy.draw(g2);
		}
		
		for (int i = 0; i < bullets.size(); i++)
		{
			Bullet tempBullet = bullets.get(i);
			tempBullet.draw(g2);
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		player.update();
		
		for (int i = 0; i < enemies.size(); i++)
		{
			Enemy tempEnemy = enemies.get(i);
			tempEnemy.update();
		}		

		for (int i = 0; i < bullets.size(); i++)
		{
			Bullet tempBullet = bullets.get(i);
			tempBullet.update();
		}
		
		repaint();
	}
	
	public void addEnemy(Enemy enemy)
	{
		enemies.add(enemy);
	}
		
	public static void removeEnemy(Enemy enemy)
	{
		enemies.remove(enemy);
	}
	
	public static ArrayList<Enemy> getEnemyList()
	{
		return enemies;
	}
	
	public static void addBullet(Bullet bullet)
	{
		bullets.add(bullet);
	}
		
	public static void removeBullet(Bullet bullet)
	{
		bullets.remove(bullet);
	}
	
	public static ArrayList<Bullet> getBulletList()
	{
		return bullets;
	}
	
	public void startGame()
	{
		for (int i = 0; i < enemyCount; i++)
		{
			addEnemy(new Enemy(rand.nextInt(800), -rand.nextInt(600)));
		}
	}
}
